<?php
class StubasicinfoController extends Controller{
	     
		  public $isActivated = 0;
          public function accessRules()
		 {
			return array(
				array(
					'allow',  // allow all users to access 'index' and 'view' actions.
					'expression'=>'$user->rights == 1',
				),
				array(
					'deny',  // deny all users
					'users'=>array('*'),
					'message'=>'You do not have access to this page ! ! ',
				),
			);
		 } 
      public function actionIndex(){
		   $this->breadcrumbs=array(
               '基本资料'=>array('index'),
               '添加资料',
             );
			 $classRooms  = Classroom::model()->findAll();
			 $dds = Directionaldistrict::model()->findAll();
			 $major = Major::model()->findAll();
			 $stu = Stuinfo::model()->find('uId = :u',array(':u'=>Yii::app()->user->getId()));
			 $stuPer = Stuperformance::model()->find('uId = :u',array(':u'=>Yii::app()->user->getId()));
			 if(!$stuPer)
			   $stuPer = new Stuperformance;
			 if($stu == NULL){
			     $stu = new Stuinfo;
		     }
		  $this->render('index',array('classRooms'=>$classRooms,'dds'=>$dds,'stu'=>$stu,'stuPer'=>$stuPer,'major'=>$major));
		  
	  }
	  public function actionUpdate(){
		  
		 $stu = Stuinfo::model()->find('uId = :u',array(':u'=>Yii::app()->user->getId()));
		  
		  if($stu == NULL){
			  $stu = new Stuinfo;
		 }
		 $_POST['stuNumber'] = Yii::app()->user->getName();
		 $stu->attributes = $_POST;
		 $stu->isPassed = 0;
		 $stu->uId = Yii::app()->user->getId();
		 if($stu->save()){
			
			 Yii::app()->user->setFlash('update','资料更新成功');
		 }else{
			
			 Yii::app()->user->setFlash('update','资料更新失败');
			 $this->logAr($stu);
		 }
		 $this->redirect(array('stubasicinfo/index'));
	  }
	  public function actionChangepassword(){
		  
		  $user = User::model()->findByPk(Yii::app()->user->getId());
		  
		  if(Yii::app()->request->getRequestType() == "POST"){
			  if($_POST['oldPassword'] == $user->password){
				  $user->password = $_POST['newPassword'];
				  if($user->save()){
					   Yii::app()->user->setFlash('update',"更新成功");
				  }else{
					   Yii::app()->user->setFlash('update',"更新失败");
					   $this->logAr($user);
				  }
				  
			  }else{
				  Yii::app()->user->setFlash('update',"原密码错误,更新失败");
				  
			  }
			  
		  }else{
			  Yii::app()->user->setFlash('update',"Invalid access");
			  
		  }
		   
		   $this->redirect(array('stubasicinfo/index'));
	  }
	  
	  public function actionUpdateimage(){
		 
		   $stu = Stuinfo::model()->find('uId = :u',array(':u'=>Yii::app()->user->getId()));
		   if($stu){
			  if(Yii::app()->request->getRequestType() == "POST"){
				  
			  if($_FILES['image']['type'] =='image/gif'|| $_FILES['image']['type'] == 'image/jpeg'|| $_FILES['image']['type'] == 'image/png'){
					  
					   $path = '/images/uploads/'.$_FILES['image']['name'];
						  if($_FILES['image']['size'] >= 2*1024*1024){
							  Yii::app()->user->setFlash('update',"图片文件过大,保存失败");
							  
						  }else{
							  $stu->pictureUrl = $path;
							  if(move_uploaded_file($_FILES["image"]["tmp_name"],'./images/uploads/'.$_FILES['image']['name'])&&$stu->save()){
								  Yii::app()->user->setFlash('update',"图片保存成功");
							  }else{
								  
								   Yii::app()->user->setFlash('update',"图片保存失败");
							  }
							  
						  }			
					  
				  }else{
					  
					  Yii::app()->user->setFlash('update',"文件类型错误，请选择 jpg,png,gif 格式，2M 以下， 谢谢合作");
				  }
				  
				   
			  }else{
				  Yii::app()->user->setFlash('update',"Invalid access");
				  
			  }
		   }else{
			   Yii::app()->user->setFlash('update',"请首先添加个人的详细资料之后再来上传图片，谢谢合作！");
			   
		   }
		  $this->redirect(array('stubasicinfo/index'));
	  }
 
 }
?>